1397B - Power Sequence - CodeForces Solution


brute force math number theory sortings *1500

Please click on ads to support us..

C++ Code:

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<unordered_map>
#define endl '\n'
using namespace std;
#define int long long
typedef pair<int, int>pii;
const int mod = 998244353;
const int N = 1e5 + 10;
int ans = 1e18;
int a[N];
signed main()
{
    ios::sync_with_stdio(0); cout.tie(0); cin.tie(0);

    int sum = 0;
    int n; cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    sort(a + 1, a + 1 + n);
    int sum0 = 0;
    for (int i = 1; i <= n; i++)
    {
        sum0 += abs(a[i] - 1);
    }
    ans = sum0;
    for (int i = 2; ; i++)
    {
        int sum1 = 0, p = 1;
        for (int j = 1; j <= n; j++)
        {
            sum1 += abs(a[j] - p);
            p *= i;
            if (sum1 > 1e15)break;
        }
        if (sum1 > 1e15)break;
        if (ans < sum1)
        {
            break;
        }
        else ans = sum1;
    }
    cout << ans << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House